package top.jamewen.framework.sbmp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.jamewen.framework.sbmp.common.api.CommonPage;
import top.jamewen.framework.sbmp.common.api.CommonResult;
import top.jamewen.framework.sbmp.common.model.UmsMenu;
import top.jamewen.framework.sbmp.model.vo.UmsMenuNodeVO;
import top.jamewen.framework.sbmp.service.UmsMenuService;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author jamewen
 * @since 2021-01-12
 */
@Slf4j
@RestController
@Api(tags = "后台菜单管理模块")
@RequestMapping("/menu")
public class UmsMenuController {
    @Autowired
    private UmsMenuService menuService;

    @ApiOperation("添加后台菜单")
    @RequestMapping(method = RequestMethod.POST)
    public CommonResult<String> create(@RequestBody UmsMenu umsMenu) {
        boolean success = menuService.create(umsMenu);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台菜单")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public CommonResult<String> update(@PathVariable Long id,
                               @RequestBody UmsMenu umsMenu) {
        boolean success = menuService.update(id, umsMenu);
        if (success) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    @ApiOperation("根据ID获取菜单详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<UmsMenu> getItem(@PathVariable Long id) {
        UmsMenu umsMenu = menuService.getById(id);
        return CommonResult.success(umsMenu);
    }

    @ApiOperation("根据ID删除后台菜单")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<String> delete(@PathVariable Long id) {
        boolean success = menuService.removeById(id);
        if (success) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failed("删除失败");
        }
    }

    @ApiOperation("分页查询后台菜单")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsMenu>> list(@PathVariable Long parentId,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsMenu> menuList = menuService.list(parentId, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("树形结构返回所有菜单列表")
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    public CommonResult<List<UmsMenuNodeVO>> treeList() {
        List<UmsMenuNodeVO> list = menuService.treeList();
        return CommonResult.success(list);
    }

    @ApiOperation("修改菜单显示状态")
    @RequestMapping(value = "/{id}/hidden", method = RequestMethod.PUT)
    public CommonResult<String> updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) {
        try {
            Boolean success = menuService.updateHidden(id, hidden);
            if(success){
                return CommonResult.success("修改成功");
            }
            return CommonResult.failed("修改失败");
        } catch (Exception e) {
            log.error("修改菜单显示状态异常：{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
    }
}

